<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Oauth2 extends CI_Controller {

    function __construct (){
	    parent::__construct();
    }
	
	public function index()
	{
		$this->load->library('WeChatCallBack');
		$this->load->model('User_model');

		$openid = $this->wechatcallback->getOpenidByPage($_GET['code']);
		
		if ($this->User_model->check_openid_exist($openid)) {
			$data = array(
					   'openid'  => $openid,
					   'logged_in' => TRUE
					);			
			$this->session->set_userdata($data);
			redirect($_SERVER['HTTP_REFERER']);
		} else {
			echo "login failed !";
		}
	}
}
